﻿using System;
using System.Collections.Generic;
using System.Text;
using SharpObjects.SearchEngine.Common.Search;

namespace SharpObjects.SearchEngine.Common.QueryConstraints
{
    [Serializable]
    public class PhraseQueryConstraint : IQueryConstraint
    {
        public PhraseQueryConstraint()
        {
            this.PhraseTerms = new List<string>();
            this.ConstraintId = Guid.NewGuid();
            this.Slop = 0;
        }

        public Guid ConstraintId { get; set; }

        public string FieldName { get; set; }
        public List<string> PhraseTerms { get; set; }
        public int Slop { get; set; } // max number irrelevant terms between matching terms
        public bool ShowFieldName { get; set; }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            for (int k = 0; k < this.PhraseTerms.Count; k++)
            {
                sb.Append(this.PhraseTerms[k]);

                if (k < (this.PhraseTerms.Count - 1))
                {
                    sb.Append(" ");
                }
            }

            if (this.ShowFieldName)
            {
                return string.Format("{0}: \"{1}\"", this.FieldName, sb.ToString());
            }
            else
            {
                return string.Format("\"{0}\"", sb.ToString());
            }
        }

    }
}
